package demo1;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
/*
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        // List<Integer> list = new ArrayList<>();
        // if (root == null) {
        //     return list;
        // }
        // list.add(root.val);
        // List<Integer> leftTree = preorderTraversal(root.left);
        // list.addAll(leftTree);

        // List<Integer> rightTree = preorderTraversal(root.right);
        // list.addAll(rightTree);

        // return list;
        List<Integer> list = new ArrayList<>();
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                list.add(cur.val);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
        return list;
    }
}*/
